﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonHelper;
using System.Reflection;

namespace WebsitSetup
{
    public partial class PublishIIS : Form
    {
        private IISManager iisManager = null;
        /// <summary>
        /// 上一步的对象
        /// </summary>
        public CreateData createData
        { get; set; }

        public PublishIIS()
        {
            InitializeComponent();
        }

        public PublishIIS(CreateData createData)
        {
            this.createData = createData;
            InitializeComponent();

            this.txtAddress.Text = CommonMethod.GetAddress();
            this.txtAddress.ReadOnly = true;
            this.txtType.Text = "1";
            this.txtType.ReadOnly = true;
            this.txtVirsualPath.Text = "WordSolution";
            var pathStr = Assembly.GetExecutingAssembly().Location;
            this.txtWebsitPath.Text = pathStr.Substring(0, pathStr.IndexOf("bin")) + "Websit";

            this.cbRead.Checked = true;
            this.cbScan.Checked = true;
            this.cbExecute.Checked = true;
            iisManager = new IISManager();

            // 如果目录已存在，则显示删除按钮
            SetDeleteVirtualPath();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要退出吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.createData.Show();
            this.Hide();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            CreateToIIS();
        }

        private void CreateToIIS()
        {
            var serverAddress = this.txtAddress.Text.Trim();
            var type = this.txtType.Text.Trim();
            var virsualPath = this.txtVirsualPath.Text.Trim();
            var websitPath = this.txtWebsitPath.Text.Trim();
            var isRead = this.cbRead.Checked;
            var isRunScript = this.cbRunScript.Checked;
            var isExcute = this.cbExecute.Checked;
            var isWrite = this.cbWrite.Checked;
            var isScan = this.cbScan.Checked;

            if (CommonMethod.checkString(virsualPath, websitPath))
            {
                // 建立一个虚拟目录对象
                VirtualDirectory vd = new VirtualDirectory();
                vd.Name = virsualPath;
                vd.Path = websitPath;
                vd.AccessExecute = isExcute;
                vd.AccessRead = isRead;
                vd.AccessScript = isRunScript;
                vd.AccessWrite = isWrite;
                vd.EnableDirBrowsing = isScan;
                string strMsg = "";
                try
                {
                    // 连接服务器
                    iisManager.Connect(serverAddress, type);
                    // 创建虚拟目录
                    iisManager.Create(vd);

                    var finish = new Finish(this, vd);
                    finish.Show();
                    this.Hide();
                    strMsg = "创建" + virsualPath + "成功";
                }
                catch (Exception ex)
                {
                    strMsg = "创建虚拟目录失败!错误如下:\n" + ex.ToString();
                }
                finally
                {
                    MessageBox.Show(strMsg, "创建信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        /// <summary>
        /// 删除虚拟目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            var virsualPath = this.txtVirsualPath.Text.Trim();
            if (CommonMethod.checkString(virsualPath))
            {
                string strMsg = "";
                try 
                { 
                    iisManager.Delete(virsualPath);
                    strMsg = "删除" + virsualPath + "成功";
                    SetDeleteVirtualPath();
                }
                catch(Exception ex)
                {
                    strMsg = "删除虚拟目录失败!错误如下:\n" + ex.ToString();   
                }
                finally
                {
                    MessageBox.Show(strMsg, "删除信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        public void SetDeleteVirtualPath()
        {
            var serverAddress = this.txtAddress.Text.Trim();
            var type = this.txtType.Text.Trim();
            var virsualPath = this.txtVirsualPath.Text.Trim();
            if (CommonMethod.checkString(serverAddress, type, virsualPath))
            {
                this.btnDelete.Enabled = iisManager.IsExist(serverAddress, type, virsualPath) ? true : false;
            }
        }

        /// <summary>
        /// 清空界面输入
        /// </summary>
        private void ClearInput()
        {
            this.txtVirsualPath.Text = "";
            this.txtWebsitPath.Text = "";
            this.cbRead.Checked = true;
            this.cbRunScript.Checked = false;
            this.cbScan.Checked = true;
            this.cbExecute.Checked = false;
            this.cbWrite.Checked = false;
        }
    }
}
